{{#if (eq orm "prisma")}}
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
{{#if (eq payments "polar")}}
import { polar, checkout, portal } from "@polar-sh/better-auth";
import { polarClient } from "./lib/payments";
{{/if}}
import prisma from "@{{projectName}}/db";

export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    {{#if (eq database "postgres")}}provider: "postgresql",{{/if}}
    {{#if (eq database "sqlite")}}provider: "sqlite",{{/if}}
    {{#if (eq database "mysql")}}provider: "mysql",{{/if}}
    {{#if (eq database "mongodb")}}provider: "mongodb",{{/if}}
  }),
  trustedOrigins: [
    process.env.CORS_ORIGIN || "",
    {{#if (or (includes frontend "native-bare") (includes frontend "native-uniwind") (includes frontend "native-unistyles"))}}
    "mybettertapp://", "exp://"
    {{/if}}
  ],
  emailAndPassword: {
    enabled: true,
  },
  {{#if (ne backend "self")}}
  advanced: {
    defaultCookieAttributes: {
      sameSite: "none",
      secure: true,
      httpOnly: true,
    },
  },
  {{/if}}
  {{#if (eq payments "polar")}}
  plugins: [
    polar({
      client: polarClient,
      createCustomerOnSignUp: true,
      enableCustomerPortal: true,
      use: [
        checkout({
          products: [
            {
              productId: "your-product-id",
              slug: "pro",
            },
          ],
          successUrl: process.env.POLAR_SUCCESS_URL,
          authenticatedUsersOnly: true,
        }),
        portal(),
      ],
    }),
  ],
  {{/if}}
});
{{/if}}

{{#if (eq orm "drizzle")}}
{{#if (or (eq runtime "bun") (eq runtime "node") (eq runtime "none"))}}
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
{{#if (eq payments "polar")}}
import { polar, checkout, portal } from "@polar-sh/better-auth";
import { polarClient } from "./lib/payments";
{{/if}}
import { db } from "@{{projectName}}/db";
import * as schema from "@{{projectName}}/db/schema/auth";

export const auth = betterAuth({
  database: drizzleAdapter(db, {
    {{#if (eq database "postgres")}}provider: "pg",{{/if}}
    {{#if (eq database "sqlite")}}provider: "sqlite",{{/if}}
    {{#if (eq database "mysql")}}provider: "mysql",{{/if}}
    schema: schema,
  }),
  trustedOrigins: [
    process.env.CORS_ORIGIN || "",
    {{#if (or (includes frontend "native-bare") (includes frontend "native-uniwind") (includes frontend "native-unistyles"))}}
    "mybettertapp://", "exp://"
    {{/if}}
  ],
  emailAndPassword: {
    enabled: true,
  },
  {{#if (ne backend "self")}}
  advanced: {
    defaultCookieAttributes: {
      sameSite: "none",
      secure: true,
      httpOnly: true,
    },
  },
  {{/if}}
  {{#if (eq payments "polar")}}
  plugins: [
    polar({
      client: polarClient,
      createCustomerOnSignUp: true,
      enableCustomerPortal: true,
      use: [
        checkout({
          products: [
            {
              productId: "your-product-id",
              slug: "pro",
            },
          ],
          successUrl: process.env.POLAR_SUCCESS_URL,
          authenticatedUsersOnly: true,
        }),
        portal(),
      ],
    }),
  ],
  {{/if}}
});
{{/if}}

{{#if (eq runtime "workers")}}
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
{{#if (eq payments "polar")}}
import { polar, checkout, portal } from "@polar-sh/better-auth";
import { polarClient } from "./lib/payments";
{{/if}}
import { db } from "@{{projectName}}/db";
import * as schema from "@{{projectName}}/db/schema/auth";
import { env } from "cloudflare:workers";

export const auth = betterAuth({
  database: drizzleAdapter(db, {
    {{#if (eq database "postgres")}}provider: "pg",{{/if}}
    {{#if (eq database "sqlite")}}provider: "sqlite",{{/if}}
    {{#if (eq database "mysql")}}provider: "mysql",{{/if}}
    schema: schema,
  }),
  trustedOrigins: [
    env.CORS_ORIGIN,
    {{#if (or (includes frontend "native-bare") (includes frontend "native-uniwind") (includes frontend "native-unistyles"))}}
    "mybettertapp://", "exp://"
    {{/if}}
  ],
  emailAndPassword: {
    enabled: true,
  },
  // uncomment cookieCache setting when ready to deploy to Cloudflare using *.workers.dev domains
  // session: {
  //   cookieCache: {
  //     enabled: true,
  //     maxAge: 60,
  //   },
  // },
  secret: env.BETTER_AUTH_SECRET,
  baseURL: env.BETTER_AUTH_URL,
  advanced: {
    defaultCookieAttributes: {
      sameSite: "none",
      secure: true,
      httpOnly: true,
    },
    // uncomment crossSubDomainCookies setting when ready to deploy and replace <your-workers-subdomain> with your actual workers subdomain
    // https://developers.cloudflare.com/workers/wrangler/configuration/#workersdev
    // crossSubDomainCookies: {
    //   enabled: true,
    //   domain: "<your-workers-subdomain>",
    // },
  },
  {{#if (eq payments "polar")}}
  plugins: [
    polar({
      client: polarClient,
      createCustomerOnSignUp: true,
      enableCustomerPortal: true,
      use: [
        checkout({
          products: [
            {
              productId: "your-product-id",
              slug: "pro",
            },
          ],
          successUrl: env.POLAR_SUCCESS_URL,
          authenticatedUsersOnly: true,
        }),
        portal(),
      ],
    }),
  ],
  {{/if}}
});
  {{/if}}
{{/if}}

{{#if (eq orm "mongoose")}}
import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
{{#if (eq payments "polar")}}
import { polar, checkout, portal } from "@polar-sh/better-auth";
import { polarClient } from "./lib/payments";
{{/if}}
import { client } from "@{{projectName}}/db";

export const auth = betterAuth({
  database: mongodbAdapter(client),
  trustedOrigins: [
    process.env.CORS_ORIGIN || "",
    {{#if (or (includes frontend "native-bare") (includes frontend "native-uniwind") (includes frontend "native-unistyles"))}}
    "mybettertapp://", "exp://"
    {{/if}}
  ],
  emailAndPassword: {
    enabled: true,
  },
  {{#if (ne backend "self")}}
  advanced: {
    defaultCookieAttributes: {
      sameSite: "none",
      secure: true,
      httpOnly: true,
    },
  },
  {{/if}}
  {{#if (eq payments "polar")}}
  plugins: [
    polar({
      client: polarClient,
      createCustomerOnSignUp: true,
      enableCustomerPortal: true,
      use: [
        checkout({
          products: [
            {
              productId: "your-product-id",
              slug: "pro",
            },
          ],
          successUrl: process.env.POLAR_SUCCESS_URL,
          authenticatedUsersOnly: true,
        }),
        portal(),
      ],
    }),
  ],
  {{/if}}
});
{{/if}}

{{#if (eq orm "none")}}
import { betterAuth } from "better-auth";
{{#if (eq payments "polar")}}
import { polar, checkout, portal } from "@polar-sh/better-auth";
import { polarClient } from "./lib/payments";
{{/if}}

export const auth = betterAuth({
  database: "", // Invalid configuration
  trustedOrigins: [
    process.env.CORS_ORIGIN || "",
    {{#if (or (includes frontend "native-bare") (includes frontend "native-uniwind") (includes frontend "native-unistyles"))}}
    "mybettertapp://", "exp://"
    {{/if}}
  ],
  emailAndPassword: {
    enabled: true,
  },
  {{#if (ne backend "self")}}
  advanced: {
    defaultCookieAttributes: {
      sameSite: "none",
      secure: true,
      httpOnly: true,
    },
  },
  {{/if}}
  {{#if (eq payments "polar")}}
  plugins: [
    polar({
      client: polarClient,
      createCustomerOnSignUp: true,
      enableCustomerPortal: true,
      use: [
        checkout({
          products: [
            {
              productId: "your-product-id",
              slug: "pro",
            },
          ],
          successUrl: process.env.POLAR_SUCCESS_URL,
          authenticatedUsersOnly: true,
        }),
        portal(),
      ],
    }),
  ],
  {{/if}}
});
{{/if}}